
/*
 *  Copyright 2007 Marc Lanctot and Marc Gendron-Bellemare
 *
 *  This file is part of OpenMTG.
 *
 *  OpenMTG is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpenMTG is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpenMTG.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.util.*; 

public class AttackPhase 
{
  private ArrayList<CombatGroup> attackers; 
  private HashMap<CombatGroup, CombatGroup> blockers; 
  private List<CombatGroup> manualDamage;

  private Player attackingPlayer, defendingPlayer; 

  public AttackPhase(Player attackingPlayer, Player defendingPlayer) 
  { 
    this.attackingPlayer = attackingPlayer;
    this.defendingPlayer = defendingPlayer;
    attackers = new ArrayList<CombatGroup>();
    blockers = new HashMap<CombatGroup,CombatGroup>(); // blocker -> attacker 
  }

	public Player getAttackingPlayer() { return attackingPlayer; }
	public Player getDefendingPlayer() { return defendingPlayer; }
	public ArrayList<CombatGroup> getAttackers() { return attackers; }
	public HashMap<CombatGroup,CombatGroup> getBlockers() { return blockers; }
  public List<CombatGroup> getManualDamage() { return manualDamage; }
  public void setManualDamage (List<CombatGroup> group)
  { manualDamage = group; }

	public void setNotAttacking() {
	  for (Card c : attackingPlayer.getCreatures()) 
	    c.setAttacking(false);
	}

  public void addAttacker(Card c) {
    attackers.add(new CombatGroup(c, attackingPlayer.getId()));
  }

	public void bandAttacker(Card bander, Card bandee)
	{
		// Not implemented! haha!
	}
	
  public void addBlocker(Card blocker, Card attacker) {
		// Find which combat group the attacker belongs to
		for (CombatGroup agroup : attackers)
		{
			// @@@ check for ids instead?
			if (agroup.aMembers.contains(attacker))
			{
				// Now find whether anyone is blocking this group
    		CombatGroup oldGroup = null;
				
				if (blockers.containsValue(agroup))
				{
					// Retrieve the blocker group who is blocking 'agroup'
					for (CombatGroup bgroup : blockers.keySet())
						if (blockers.get(bgroup) == agroup)
						{
							oldGroup = bgroup;
							break;
						}
				}
				
				CombatGroup newGroup = new 
					CombatGroup(blocker, defendingPlayer.getId());
				if (oldGroup != null) // Add the old members to this new group
				{
					for (Card c : oldGroup.aMembers)
						newGroup.aMembers.add(c);
				}
			
				// This is fishy - should we create a new agroup?
				blockers.put(newGroup, agroup);
				blockers.remove(oldGroup);
			}
		}
  }

  public CombatGroup getManualDamageGroup(int victim)
  {
    if (manualDamage == null) return null;

    // Go through the combat groups and find which one contains this
    // victim
    for (CombatGroup group : manualDamage)
    {
      if (group.getCard(victim) != null)
        return group;
    }

    return null;
  }
  
  public void removeManualDamageGroup(CombatGroup group)
  {
    // sanity check - make sure the group's damage has been assigned
    if (group.getDamage() > 0)
      throw new RuntimeException("Attempting to remove a combat group with "+
        "damage to be assigned");
    else
      manualDamage.remove(group);
  }
 
  public boolean isBlocking(int iid)
  {
    for (CombatGroup group : blockers.keySet())
    {
      if (group.getCard(iid) != null)
        return true;
    }

    return false;
  }

  public void resolve() 
  {
  }

  /**
   * Returns a string describing the results of the attack phase. 
   *
   * String is of the form S1
   *
   * where S1 is of the form pid=lifepoints pid=lifepoints pid=lifepoints ... 
   */
  public String getResults() 
  {
    return (        (attackingPlayer.getId() + "=" + attackingPlayer.getLifePoints())
            + "," + (defendingPlayer.getId() + "=" + defendingPlayer.getLifePoints())  );
  }
}
